package com.codemobiles.droidslator.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.view.View.OnClickListener;
import android.widget.ImageView;

import com.codemobiles.droidslator.R;
import com.codemobiles.droidslator.util.MyUtil;

public class SplashActivity extends Activity {
  private ImageView skipBnt;
  private Thread splashTread;
  private int check = 0;
  
  //handler for splash screen
  public Handler mHandler = new Handler() {
    public void handleMessage(Message msg) {
      switch (msg.what) {
      case 0:
        check = 1;
        Log.i("SDKVersion", Build.VERSION.SDK);
        // if sdk 1.5 use old droidslator old version
        if (Build.VERSION.SDK.equals("3") == true) {
          startActivity(new Intent("com.codemobiles.droidslator.DroidslatorOldVer"));
        } else {
          startActivity(new Intent("com.codemobiles.droidslator.Droidslator"));
        }
        finish();
        break;
      case 1:
        if(check == 0){
          Log.i("SDKVersion", Build.VERSION.SDK);
          // if sdk 1.5 use old droidslator old version
          if (Build.VERSION.SDK.equals("3") == true) {
            startActivity(new Intent("com.codemobiles.droidslator.DroidslatorOldVer"));
          } else {
            startActivity(new Intent("com.codemobiles.droidslator.Droidslator"));
          }
          finish();
        }
        break;
      }
    } 
  };


  @Override
  protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    setContentView(R.layout.splash);
    MyUtil.init(this);
    
 // initial widgets
    bindWidgets();
    setWidgetListener();

    // show your progress dialog
    splashTread = new Thread() {
      @Override
      public void run() {
        try {
          for (int i = 0; i < 10; i++) {
            sleep(200);
          }
        } catch (InterruptedException e) {
          // do nothing
        }finally{
          mHandler.sendEmptyMessage(1);
        }
      }
    };
    splashTread.start(); 
  }
  
    /**
     * Call this method to bind widgets
     */
    private void bindWidgets() {
        skipBnt = (ImageView) findViewById(R.id.skip);
    }
    
    /**
     * Call this method to set widget listener
     */
    private void setWidgetListener() {
        skipBnt.setOnClickListener(new OnClickListener() {
          @Override
          public void onClick(View v) {
            mHandler.sendEmptyMessage(0);
          }
        }); 
    }
    @Override
    protected void onDestroy() {
      // TODO Auto-generated method stub
      super.onDestroy();
      splashTread.stop();
    }
}
